﻿using System.Collections.Concurrent;
using Basic.WxCollect;
using Basic.WxExtendService.Interface;
using Basic.WxModel.WxPlatform;
using WeDonekRpc.Client;
using WeDonekRpc.Client.Attr;
using WeDonekRpc.Client.Ioc;

namespace Basic.WxExtendService.Extend
{
    [ClassLifetimeAttr(ClassLifetimeType.SingleInstance)]
    internal class WxPlatformCacheService : IWxPlatformCacheService
    {
        private static readonly ConcurrentDictionary<string, WxPlatformDto> _WxPlatformId = new ConcurrentDictionary<string, WxPlatformDto>();

        public WxPlatformDto GetPlatform (string appId)
        {
            if (_WxPlatformId.TryGetValue(appId, out WxPlatformDto dto))
            {
                return dto;
            }
            using (IocScope scope = RpcClient.Ioc.CreateScore())
            {
                IWxPlatformCollect wxMer = scope.Resolve<IWxPlatformCollect>();
                dto = wxMer.GetPlatform(appId);
                _ = _WxPlatformId.TryAdd(appId, dto);
                return dto;
            }
        }
    }
}
